<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <input type="text" id="copy1" />
      <button id="button1">复制</button>
    </div>
    <pre>
    <code>
    document.querySelector('#button1').addEventListener('click', function () {
        var input = document.querySelector('#copy1'); input.select();
        document.execCommand('copy');
    });
    </code>
    </pre>
    <div>
      <input type="text" id="copy2" />
      <button id="button2">复制</button>
    </div>
    <pre>
    <code>
      document.querySelector('#button2').addEventListener('click', function () {
        var input = document.querySelector('#copy2');
        navigator.permissions
          .query({ name: 'clipboard-write' })
          .then((result) => {
            if (result.state !== 'denied') {
              navigator.clipboard.writeText(input.value).then(
                () => {
                  console.log('复制成功！');
                },
                () => {
                  console.log('复制失败！');
                }
              );
            }
            // 如果拒绝授予此权限则什么也不做。
          });
      });
    </code>
    </pre>
    <script>
      document.querySelector('#button1').addEventListener('click', function () {
        var input = document.querySelector('#copy1');
        input.select();
        document.execCommand('copy');
      });

      document.querySelector('#button2').addEventListener('click', function () {
        var input = document.querySelector('#copy2');
        navigator.permissions
          .query({ name: 'clipboard-write' })
          .then((result) => {
            if (result.state !== 'denied') {
              navigator.clipboard.writeText(input.value).then(
                () => {
                  console.log('复制成功！');
                },
                () => {
                  console.log('复制失败！');
                }
              );
            }
            // 如果拒绝授予此权限则什么也不做。
          });
      });
    </script>
  </body>
</html>
